package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactoryLogin;
import com.lineage.config.Config;
import com.lineage.server.IdFactory;
import com.lineage.server.datatables.storage.ServerStorage;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerTable implements ServerStorage {
  private static final Log _log = LogFactory.getLog(ServerTable.class);
  
  private int _maxId;
  
  private int _minId;
  
  private static int _srcminId = 10000;
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "SELECT * FROM `server_info`";
      ps = co.prepareStatement("SELECT * FROM `server_info`");
      rs = ps.executeQuery();
      boolean isInfo = false;
      while (rs.next()) {
        int id = rs.getInt("id");
        if (Config.SERVERNO == id) {
          isInfo = true;
          int minid = rs.getInt("minid");
          int maxid = rs.getInt("maxid");
          this._minId = minid;
          if (this._minId < _srcminId)
            this._minId = _srcminId; 
          this._maxId = maxid;
          set_start();
        } 
      } 
      if (!isInfo) {
        createServer();
        this._minId = _srcminId;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
      SQLUtil.close(rs);
    } 
    _log.info("载入服务器存档资料完成  (" + timer.get() + "ms)");
  }
  
  private static void createServer() {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "INSERT INTO `server_info` SET `id`=?,`minid`=?,`maxid`=?,`start`=?";
      ps = cn.prepareStatement("INSERT INTO `server_info` SET `id`=?,`minid`=?,`maxid`=?,`start`=?");
      int i = 0;
      ps.setInt(++i, Config.SERVERNO);
      ps.setInt(++i, _srcminId);
      ps.setInt(++i, 0);
      ps.setBoolean(++i, true);
      ps.execute();
      _log.info("新服务器资料表建立 - 编号:" + Config.SERVERNO);
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private void set_start() {
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "UPDATE `server_info` SET `start`=? WHERE `id`=?";
      pstm = con.prepareStatement("UPDATE `server_info` SET `start`=? WHERE `id`=?");
      int i = 0;
      pstm.setBoolean(++i, true);
      pstm.setInt(++i, Config.SERVERNO);
      pstm.execute();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public void isStop() {
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "UPDATE `server_info` SET `maxid`=?,`start`=? WHERE `id`=?";
      pstm = con.prepareStatement("UPDATE `server_info` SET `maxid`=?,`start`=? WHERE `id`=?");
      int i = 0;
      pstm.setInt(++i, IdFactory.get().maxId());
      pstm.setBoolean(++i, false);
      pstm.setInt(++i, Config.SERVERNO);
      pstm.execute();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public int minId() {
    return this._minId;
  }
  
  public int maxId() {
    return this._maxId;
  }
}
